<% content_for :head do %>
  <%= tag.meta property: "og:type", content: "website" %>
  <%= tag.meta property: "og:image", content: model_model_file_url(@model, @model.preview_file, format: @model.preview_file.extension) if @model.preview_file&.is_image? && !@model.sensitive %>
  <%= tag.meta name: "description", content: truncate(sanitize(@model.notes), length: 80) if @model.notes.present? %>
  <%= tag.meta name: "fediverse:creator", content: @model.creator.federails_actor.at_address if @model.creator %>
  <%= tag.link rel: "alternate", type: Mime[:oembed], href: model_url(@model, format: :oembed), title: @model.name %>
<% end %>

<div itemscope itemtype="https://schema.org/3DModel">
  <%= turbo_stream_from @model %>
  <h1>
    <%= policy(@model).edit? ? EditableSpan(fieldname: "model[name]", path: model_path(@model), text: @model.name) : @model.name %>
    <%= link_to Icon(icon: "search", label: t(".search")),
          "https://yeggi.com/q/#{ERB::Util.url_encode(@model.name)}/",
          class: "btn btn-outline", target: "manyfold_search", rel: "noreferrer" %>
  </h1>

  <div class="row row-cols-md-2 mt-2">
    <div class="col-md-9" id="item_list">
      <% if @locked_files > 0 %>
        <div class="alert alert-info"><%= t(".preview", count: @locked_files) %></div>
      <% end %>

      <%= ImageCarousel(images: @images) %>

      <%= card(:secondary) do %>
        <p class="card-text" itemprop="description"><%= markdownify @model.notes %></p>
      <% end if @model.notes.present? %>

      <% if @num_files > 0 %>
        <h2><a name="files"><%= t(".files") %></a></h2>
        <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 mb-4">
          <%= render partial: "file", collection: @groups.delete(nil) %>
        </div>
        <% @groups.each_pair do |group, files| %>
          <h3><a name="<%= group.parameterize %>"><%= group.strip.careful_titleize %>*</a></h3>
          <div class="row  row-cols-1 row-cols-md-2 row-cols-lg-3 mb-4">
            <%= render partial: "file", collection: files %>
          </div>
        <% end %>
      <% end %>
    </div>
    <div class="col-md-3" id="sidebar">
      <%= card :secondary, t(".model_details") do %>
        <div class="details-table">
          <%= AccessIndicator(object: @model, text: false) %>
          <%= AccessIndicator(object: @model, text: true, icon: false) %>
          <% if @model.creator %>
            <div><%= Icon icon: "person", label: Creator.model_name.human %></div>
            <div><%= link_to @model.creator.name, @model.creator, itemprop: "author" %></div>
          <% end %>
          <% if @model.collection %>
            <div><%= Icon icon: "collection", label: Collection.model_name.human(count: 100) %></div>
            <div><%= link_to @model.collection.name, models_path({collection: @model.collection}.merge(@filter&.to_params)) %></div>
          <% end %>
          <% if SiteSettings.show_libraries || current_user&.is_administrator? %>
            <div><%= Icon icon: "boxes", label: Library.model_name.human %></div>
            <div><%= link_to @model.library.name, models_path({library: @model.library}.merge(@filter&.to_params)) %></div>
          <% end %>
          <% if @model.license %>
            <div><%= Icon icon: "card-heading", label: t(".license") %></div>
            <div>
              <%= Spdx.licenses[@model.license]&.fetch("reference") ?
                link_to(t_license(@model.license), Spdx.licenses[@model.license]["reference"], itemprop: "license") :
                t_license(@model.license) %>
            </div>
          <% end %>
          <% if @model.sensitive %>
            <div><%= Icon(icon: "explicit", label: Model.human_attribute_name(:sensitive)) %></div>
            <div>
              <%= Model.human_attribute_name(:sensitive) %>
            </div>
          <% end %>
          <% if current_user&.is_administrator? %>
            <div><%= Icon icon: "folder", label: t(".path") %></div>
            <div>
              <%= content_tag(:code, class: "path") { safe_join @model.path.split("/"), safe_join([tag.wbr, "/"]) } %>
            </div>
          <% end %>
          <div><%= Icon icon: "tag", label: t(".tags") %></div>
          <div><%= render "tag_list", tags: @model.tags.order(taggings_count: :desc, name: :asc), filter: @filter %></div>
          <% unless @model.links.empty? %>
            <div><%= Icon icon: "link-45deg", role: "presentation", label: Model.human_attribute_name(:links) %></div>
            <%= LinkList links: @model.links, icons: false %>
          <% end %>
          <% if SiteSettings.social_enabled? %>
            <% if SiteSettings.federation_enabled? %>
              <div>⁂</div>
              <div>
                <% if @model.remote? %>
                  <small><%= link_to @model.federails_actor.at_address, @model.federails_actor.profile_url, target: "new" %></small>
                <% else %>
                  <small>
                    <%= CopyableText(text: @model.federails_actor.at_address, label: t("general.copy_fediverse_handle")) %>
                  </small>
                <% end %>
              </div>
            <% end %>
            <div><%= Icon icon: "people", label: t(".followers") %></div>
            <div>
              <%= t("general.followers", count: @model.followers.count) %>
              <div class="float-end">
                <%= FollowButton(follower: current_user, target: @model) %>
              </div>
            </div>
          <% end %>
        </div>
      </div>
    </div>
    <div class="card-footer">
      <%= DownloadButton(model: @model) %>
      <div class="float-end">
        <%= BurgerMenu id: "model-actions-menu", data: tour_attributes(id: "model-actions-menu", title: translate(".menu.tour.title"), description: translate(".menu.tour.description")) do %>
          <%= DropdownItem(icon: "pencil", label: translate("general.edit"), path: edit_model_path(@model), aria_label: translate("components.model_card.edit_button.label", name: @model.name)) if policy(@model).edit? %>
          <%= DropdownItem(icon: "trash", label: translate("general.delete"), path: model_path(@model), method: :delete, aria_label: translate("components.model_card.delete_button.label", name: @model.name), confirm: t("models.destroy.confirm")) if policy(@model).destroy? %>
          <%= DropdownDivider() %>
          <%= DropdownItem(icon: "pencil", label: t(".files_card.bulk_edit"), path: bulk_edit_model_model_files_path(@model)) if policy(@model).edit? %>
          <%= DropdownItem(icon: "radar", label: t(".rescan"), path: scan_model_path(@model), method: :post) if policy(@model).scan? %>
          <%= DropdownDivider() %>
          <%= DropdownItem(icon: "flag", label: translate("general.report", type: ""), path: new_model_report_path(@model)) if SiteSettings.multiuser_enabled? %>
        <% end %>
      <% end %>

      <% if policy(:problem).show? %>
        <% if !@model.parents.empty? && policy(@model).merge? %>
          <%= card :danger, t(".merge.heading") do %>
            <p>
              <%= t(".merge.warning") %>
            </p>
            <strong><%= t(".merge.with") %>:</strong>
            <% @model.parents.each do |parent| %>
              <%= DoButton(icon: "union", label: parent.name, href: merge_models_path(target: parent.public_id, models: [@model.public_id]), method: :post, variant: "danger") %>
            <% end %>
          <% end %>
        <% end %>
        <%= render partial: "problems_card", locals: {problems: @model.problems.visible(problem_settings)} %>
      <% end %>

      <%= card :secondary, t(".files_card.heading") do %>
        <a href="#files">Top</a>
        <ul>
          <% @groups.each_pair do |group, files| %>
            <li><a href="#<%= group.parameterize %>"><%= group.strip.careful_titleize %>*</a></li>
          <% end %>
        </ul>
      <% end %>

      <% if policy(@model).upload? %>
        <%= card :secondary, t(".upload_card.heading") do %>
          <%= form_with url: model_model_files_path(@model), id: "upload-form", class: "d-grid" do |form| %>

            <%= content_tag :div, nil, class: "mb-3", data: {
                  controller: "upload",
                  action: "turbo:morph@window->upload#reconnect",
                  max_file_size: SiteSettings.max_file_upload_size,
                  allowed_file_types: input_accept_string,
                  upload_endpoint: upload_path
                } %>
            <%= submit_tag translate(".submit"), class: "btn btn-primary d-block" %>
          <% end %>
        <% end %>
      <% end %>
    </div>
  </div>
</div>
